package com.huike.web.controller.system;

import com.huike.clues.result.RoleDeptTreeAjaxResult;
import com.huike.clues.service.ISysDeptService;
import com.huike.common.constant.UserConstants;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.domain.TreeSelect;
import com.huike.common.core.domain.entity.SysDeptDTO;
import com.huike.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.util.ArrayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.List;

/**
 * @Description 部门管理相关接口
 * @Date 2023-10-10
 */
@RequestMapping("/system/dept")
@RestController
@Api("部门相关接口")
public class SysDeptController extends BaseController {
    @Autowired
    private ISysDeptService deptService;
    @GetMapping("/list")
    @ApiOperation("分页查询部门列表")
    public AjaxResult list(SysDeptDTO dept){
        List<SysDeptDTO> depts = deptService.selectDeptList(dept);
        return AjaxResult.success(depts);
    }
    @GetMapping("/list/exclude/{deptld}")
    @ApiOperation("查询部门列表")
    public AjaxResult excludeChild(@PathVariable Long deptld){
        SysDeptDTO sysDeptDTO = new SysDeptDTO();
        List<SysDeptDTO> depts = deptService.selectDeptList(sysDeptDTO);
        Iterator<SysDeptDTO> it = depts.iterator();
        while (it.hasNext()){
            SysDeptDTO d = it.next();
            if (d.getDeptId().intValue() == deptld || ArrayUtils.contains(StringUtils.split(d.getAncestors(),","),deptld + "")){
                it.remove();
            }
        }
        return AjaxResult.success(depts);
    }
    @GetMapping("/{deptId}")
    @ApiOperation("根据id查询详细信息")
    public AjaxResult gitinfo(@PathVariable Long deptId){
        SysDeptDTO deptDTO = deptService.selectDeptById(deptId);
        return AjaxResult.success(deptDTO);
    }
    /**
     * 获取部门下拉树列表
     */
    @ApiOperation("获取部门下拉树列表")
    @GetMapping("/treeselect")
    public AjaxResult<List<TreeSelect>> treeselect(SysDeptDTO dept)
    {
        System.out.println(" treeselect----------------------");
        List<SysDeptDTO> depts = deptService.selectDeptList(dept);
        return AjaxResult.success(deptService.buildDeptTreeSelect(depts));
    }
    /**
     * 加载对应角色部门列表树
     */
    @ApiOperation("获取对应角色部门列表树")
    @GetMapping(value = "/roleDeptTreeselect/{roleId}")
    public RoleDeptTreeAjaxResult roleDeptTreeselect(@PathVariable("roleId") Long roleId)
    {
        System.out.println(" roleDeptTreeselect----------------------");
        List<SysDeptDTO> depts = deptService.selectDeptList(new SysDeptDTO());
        RoleDeptTreeAjaxResult ajax = RoleDeptTreeAjaxResult.success();
        ajax.setCheckedKeys( deptService.selectDeptListByRoleId(roleId));
        ajax.setDepts(deptService.buildDeptTreeSelect(depts));
        return ajax;
    }
    @PostMapping
    @ApiOperation("新增部门")
    public AjaxResult add(@RequestBody SysDeptDTO dept){
        if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept))){
            return AjaxResult.error("新增部门" + dept.getDeptName() + "失败,该部门已存在");
        }
        dept.setCreateBy(SecurityUtils.getUsername());
        return toAjax(deptService.insertDept(dept));
    }
    @PutMapping
    @ApiOperation("修改部门")
    public AjaxResult edit(@RequestBody SysDeptDTO dept){
        if (com.huike.common.utils.StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus())
                && deptService.selectNormalChildrenDeptById(dept.getDeptId()) > 0)
        {
            return AjaxResult.error("该部门包含未停用的子部门！");
        }
        dept.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(deptService.updateDept(dept));
    }
    @DeleteMapping("/{deptId}")
    @ApiOperation("删除部门")
    public AjaxResult remove(@PathVariable Long deptId){
        if (deptService.hasChildByDeptId(deptId)){
            return AjaxResult.error("该部门存在下级部门,无法删除");
        }
        if (deptService.checkDeptExistUser(deptId)){
            return AjaxResult.error("该部门存在用户,无法删除");
        }
        return toAjax(deptService.deleteDeptById(deptId));
    }
    @GetMapping("/treeAnduser")
    @ApiOperation("获取部门及人员")
    public AjaxResult<List<TreeSelect>> treeAnduser(){
        List<SysDeptDTO> depts = deptService.selectDeptAndUserList();
        List<TreeSelect> treeSelectList=  deptService.buildDeptAndUserTree(depts);
        return AjaxResult.success(treeSelectList);
    }
}
